****This file runs the regressions reported in Tables O5 and O6 of the appendix
**Curtis RESTAT
**Change to directory where data and code were downloaded
**cd 

set matsize 1500
clear all
set more off

use cbp_st_tableO5O6,clear

drop if enint==0 /*keeps only MFTG Industries*/
drop if fipstate=="11" /*Drops DC*/

do dataclean_tableO5O6.do

gen empweight2=emp if year==1998
bysort stateind:carryforward empweight2,replace

so stateind year
egen empweight3t=mean(emp),by(stateind post)
replace empweight3t=. if post==1
egen empweight3=max(empweight3t),by(stateind)

gen eaststr="east" if east==1
replace eaststr="west" if eaststr==""
gen eastind=eaststr+naics

drop yeardum*
qui tab year,gen(yeardum)


gen trend=year-1997
qui tab fipstate,gen(STdum)
foreach var of varlist ind1-ind21 {
qui gen trend`var'=`var'*trend
}

foreach x of varlist STdum1-STdum49 {
qui gen trend`x'=`x'*trend
}

/* Generate Seperate East/West Trends by Industry*/
forvalues i = 1(1)21 {
  qui gen eindtrend`i' = trendind`i'*east
 }
forvalues i = 1(1)21 {
  qui gen windtrend`i' = trendind`i'*((east-1)^2)
 }
tostring east,replace
gen yearindreg=yearind+"_"+east


qui tab yearind,gen(yearinddum)
qui tab STyear, gen(STyeardum)
qui tab stateind, gen(stateinddum)

/* Defines RHS variables to be included in every regression*/
mac def rhs_vars "postxint intxeast postxeast  "

so naics
merge m:1 naics using "nei_reg_merge_tableO5O6.dta"
drop _m

destring east,replace
gen noxintxeast=nox_int*east
gen postxnoxint=nox_int*post
gen postxeastxnoxint=nox_int*post*east

gen regperxeast=reg_per*east
gen postxregper=reg_per*post
gen postxeastxregper=reg_per*post*east

gen perplantxeast=per_plant*east
gen postxperplant=per_plant*post
gen postxeastxperplant=per_plant*post*east

gen noxintper=nox_int*reg_per
gen noxintperxeast=noxintper*east
gen postxnoxintper=noxintper*post
gen postxeastxnoxintper=noxintper*post*east

gen dirty=1 if inlist(naics,"321","326","325","324","322","327","331")
replace dirty=0 if dirty==.
gen postxeastxdirty=post*east*dirty
gen postxdirty=post*dirty
gen eastxdirty=east*dirty

so naics
merge m:1 naics using footloosevars_tableO5O6.dta
drop _m

gen postxeastxfixedcosts=post*east*fixedcosts
gen postxfixedcosts=post*fixedcosts
gen eastxfixedcosts=east*fixedcosts

gen postxeastxcif=post*east*cif
gen postxcif=post*cif
gen eastxcif=east*cif



mac def noxintvars "postxeastxnoxint postxnoxint noxintxeast postxeast"
mac def regpervars "postxeastxregper postxregper regperxeast postxeast"
mac def perplantvars "postxeastxperplant postxperplant perplantxeast postxeast"
mac def noxintpervars "postxeastxnoxintper postxnoxintper noxintperxeast postxeast"
mac def dirtyvars "postxeastxdirty postxdirty eastxdirty postxeast"
mac def footloosevars1 "postxeastxint postxeastxfixedcosts postxfixedcosts eastxfixedcosts postxint postxeast intxeast "
mac def footloosevars2 "postxeastxint postxeastxcif postxcif eastxcif postxint postxeast intxeast "
mac def footloosevars3 "postxeastxint postxeastxfixedcosts postxeastxcif postxfixedcosts eastxfixedcosts postxcif eastxcif postxint postxeast intxeast "


/*Table O5 Panel A*/
*Column 1
qui eststo:areg lemp $noxintvars yeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 2
qui eststo:areg lemp $noxintvars yearinddum* trendSTdum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 3
qui eststo:areg lemp $noxintvars yearinddum* STyeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 4
qui eststo:areg lemp $noxintvars yeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 5
qui eststo:areg lemp $noxintvars yearinddum* trendSTdum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 6
qui eststo:areg lemp $noxintvars yearinddum* STyeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)

outreg2 [est1 est2 est3 est4 est5 est6] using "TableO5a_noxintregs.tex",keep(postxeastxnoxint postxeast) nocons replace
eststo clear

/*Table O5 Panel B*/
*Column 1
qui eststo:areg lemp $perplantvars yeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 2
qui eststo:areg lemp $perplantvars yearinddum* trendSTdum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 3
qui eststo:areg lemp $perplantvars yearinddum* STyeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 4
qui eststo:areg lemp $perplantvars yeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 5
qui eststo:areg lemp $perplantvars yearinddum* trendSTdum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 6
qui eststo:areg lemp $perplantvars yearinddum* STyeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)

outreg2 [est1 est2 est3 est4 est5 est6] using "TableO5b_perestabregs.tex",keep(postxeastxperplant postxeast) nocons replace
  
eststo clear
/*Table O5 Panel C*/
*Column 1
qui eststo:areg lemp $regpervars yeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 2
qui eststo:areg lemp $regpervars yearinddum* trendSTdum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 3
qui eststo:areg lemp $regpervars yearinddum* STyeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 4
qui eststo:areg lemp $regpervars yeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 5
qui eststo:areg lemp $regpervars yearinddum* trendSTdum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 6
qui eststo:areg lemp $regpervars yearinddum* STyeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)

outreg2 [est1 est2 est3 est4 est5 est6] using "TableO5c_pernoxregs.tex",keep(postxeastxregper postxeast) nocons replace
  
eststo clear



eststo clear

/*Table O6 Panel A*/
*Column 1
qui eststo:areg lemp $footloosevars3 yeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 2
qui eststo:areg lemp $footloosevars3 yearinddum* trendSTdum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 3
qui eststo:areg lemp $footloosevars3 yearinddum* STyeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 4
qui eststo:areg lemp $footloosevars3 yeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 5
qui eststo:areg lemp $footloosevars3 yearinddum* trendSTdum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 6
qui eststo:areg lemp $footloosevars3 yearinddum* STyeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)

outreg2 [est1 est2 est3 est4 est5 est6] using "TableO6a_footlooseregs.tex",keep(postxeastxint postxeastxfixedcosts postxeastxcif) nocons replace
  
eststo clear

/*Table O6 Panel B*/
*Column 1
qui eststo:areg lemp $footloosevars1 yeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 2
qui eststo:areg lemp $footloosevars1 yearinddum* trendSTdum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 3
qui eststo:areg lemp $footloosevars1 yearinddum* STyeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 4
qui eststo:areg lemp $footloosevars1 yeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 5
qui eststo:areg lemp $footloosevars1 yearinddum* trendSTdum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 6
qui eststo:areg lemp $footloosevars1 yearinddum* STyeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)

outreg2 [est1 est2 est3 est4 est5 est6] using "TableO6b_footlooseregs.tex",keep(postxeastxint postxeastxfixedcosts) nocons replace
  
eststo clear

/*Table O6 Panel C*/
*Column 1
qui eststo:areg lemp $footloosevars2 yeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 2
qui eststo:areg lemp $footloosevars2 yearinddum* trendSTdum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 3
qui eststo:areg lemp $footloosevars2 yearinddum* STyeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 4
qui eststo:areg lemp $footloosevars2 yeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 5
qui eststo:areg lemp $footloosevars2 yearinddum* trendSTdum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
*Column 6
qui eststo:areg lemp $footloosevars2 yearinddum* STyeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)

outreg2 [est1 est2 est3 est4 est5 est6] using "TableO6c_footlooseregs.tex",keep(postxeastxint postxeastxcif) nocons replace
  
eststo clear




